package com.qbgs.security;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;


/**
 * MD5摘要算法
 * @author lyh
 * @version 2011-11-01
 * @see MD5
 * @since
 */
public class MD5
{

    /**
     * 日志
     */
    private static final Logger logger = Logger.getLogger(MD5.class);

    /**
     * 
     * Description: 转换成MD5密文
     * 
     * @param plainText 明文
     * @return String 进行过MD5算法后的密文
     * @see
     */
    public static String toMD5(String plainText)
    {
        try
        {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++ )
            {
                i = b[offset];
                if (i < 0)
                {
                    i += 256;
                }
                if (i < 16)
                {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            String str = buf.toString();
            return str;
        }
        catch (NoSuchAlgorithmException e)
        {
            logger.error("NoSuchAlgorithmException:", e);
        }
        return null;
    }

    /**
     * Test
     * Description: <br>
     * 
     * @param args 
     * @see
     */
    public static void main(String[] args)
    {
        logger.info(MD5.toMD5("admin"));
    }
}
